#include "FileUnit.h"

void Accomplish(char Level)
  {
    errno_t err;
    FILE *pFile;
    err = fopen_s(&pFile, "GameLevel.dat", "w");

    if (err == 0)
      {
        fputc(Level + 1, pFile);
        fclose(pFile);
      }
  }

int GameStatus()
  {
    errno_t err;
    FILE *pFile = NULL;
    char Status;
    err = fopen_s(&pFile, "GameStatus.dat", "r");
    if (err == 0)
      {
        Status = fgetc(pFile);
        fclose(pFile);//When it opened successfully, this is the time to close file;
      }
    return Status;
  }
void WriteStatus(char Status)
  {
    errno_t err;
    FILE *pFile = NULL;
    err = fopen_s(&pFile, "GameStatus.dat", "w");

    if (err == 0)
      {
        fputc(Status, pFile);
        fclose(pFile);
      }
  }
int GameLevel()
  {
    errno_t err;
    FILE *pFile = NULL;
    char Level;
    err = fopen_s(&pFile, "GameLevel.dat", "r");
    if (err == 0)
      {
        Level = fgetc(pFile);
        fclose(pFile);
      }

    if (err == 2)
      {
        InvalidLevel();
        return -1;
      }

    return Level;
  }

void ReadMapN(char *pMap, int Level)
  {
    errno_t err;
    FILE *pFile = NULL;
    if (Level == 0)
        err = fopen_s(&pFile, "Exp0.map", "r");
    if (Level == 1)
        err = fopen_s(&pFile, "Exp1.map", "r");
    if (Level == 2)
        err = fopen_s(&pFile, "Exp2.map", "r");
    if (Level == 3)
        err = fopen_s(&pFile, "Exp3.map", "r");

    if (err == 0)
      {
        fgets(pMap, 350, pFile);
        fclose(pFile);
      }
  }
void IntergrityChecking()
  {
    errno_t err;
    FILE *pFile = NULL;

    err = fopen_s(&pFile, "GameLevel.dat", "r");
    if (err == 2)
      GameLevelLost();
    if (err == 0)
      fclose(pFile);


    err = fopen_s(&pFile, "GameStatus.dat", "r");
    if (err == 2)
      GameStatusLost();
    if (err == 0)
      fclose(pFile);

    err = fopen_s(&pFile, "Exp0.map", "r");
    if (err == 2)
      Exp0MapLost();
    if (err == 0)
      fclose(pFile);

    err = fopen_s(&pFile, "Exp1.map", "r");
    if (err == 2)
      Exp1MapLost();
    if (err == 0)
      fclose(pFile);

    err = fopen_s(&pFile, "Exp2.map", "r");
    if (err == 2)
      Exp2MapLost();
    if (err == 0)
      fclose(pFile);

    err = fopen_s(&pFile, "Exp3.map", "r");
    if (err == 2)
      Exp3MapLost();
    if (err == 0)
      fclose(pFile);
  }
